﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using Warehouse.Application.Dto.Input;
using Warehouse.Application.Dto.Output;
using Warehouse.Application.Services.Storage;
using Warehouse.Domain.Entities;
using Warehouse.Result;

namespace Warehouse.Api.Controllers
{
    /// <summary>
    /// 货架/库区
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    
    public class StorageController : ControllerBase
    {
        private readonly IStorageAppService _storageAppService;
        private readonly ILogger<StorageController> _logger;

        public StorageController(IStorageAppService storageAppService, ILogger<StorageController> logger)
        {
            _storageAppService = storageAppService;
            _logger = logger;
        }

        /// <summary>
        /// 创建货架/库区
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> CreateStore(CreateStoreInput input)
        {
            return _storageAppService.CreateStore(input);
        }



        /// <summary>
        /// 获取货架/库区列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiPaing<StorageOutput> GetStorage([FromQuery]PageInput input)
        {
            return _storageAppService.GetStorage(input);
        }


        /// <summary>
        /// 所属仓库下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiPaing<WareHouseModel> GetAllWareHouse()
        {
            return _storageAppService.GetAllWareHouse();
        }


        /// <summary>
        /// 删除货架/库区详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> DeltetStorage(long Id)
        {
            return _storageAppService.DeltetStorage(Id);
        }

        /// <summary>
        /// 修改货架/库区详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdateStorage(UpdateStorageInput input)
        {
            return _storageAppService.UpdateStorage(input);
        }


        /// <summary>
        /// 获取一条货架/库区详情用来回显操作
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<StorageModel> GetModelStorage(long Id)
        {
            return _storageAppService.GetModelStorage(Id);
        }


        /// <summary>
        /// 货架/库区导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Derive()
        {
            var book = new XSSFWorkbook();
            var a = book.CreateSheet("标题");
            var row = a.CreateRow(0);
            row.CreateCell(0).SetCellValue("货架/库区编号");
            row.CreateCell(1).SetCellValue("货架/库区编码");
            row.CreateCell(2).SetCellValue("货架/库区名称");
            row.CreateCell(3).SetCellValue("仓库编码");
            row.CreateCell(4).SetCellValue("仓库名称");
            row.CreateCell(5).SetCellValue("描述");

            var index = 1;
            var bs = _storageAppService.GetStorage(new PageInput { PageIndex = 1, PageSize = 1000 });
            foreach (var s in bs.PageDatas)
            {
                var rows = a.CreateRow(index++);
                rows.CreateCell(0).SetCellValue(s.Id);
                rows.CreateCell(1).SetCellValue(s.StorageCoding);
                rows.CreateCell(2).SetCellValue(s.StorageName);
                rows.CreateCell(3).SetCellValue(s.WareHouseCoding);
                rows.CreateCell(4).SetCellValue(s.WareHouseName);
                rows.CreateCell(5).SetCellValue(s.StorageDescribe);
            }

            byte[] sb = null;
            using (var ms = new MemoryStream())
            {
                book.Write(ms);
                sb = ms.ToArray();
            }
            return File(sb, System.Net.Mime.MediaTypeNames.Application.Octet, "货架/库区打印.xlsx");

        }









    }
}
